package cn.ronghuanet.car.controller;

import cn.ronghuanet.car.dto.CarResourcesDTO;
import cn.ronghuanet.car.service.ICarResourcesService;
import cn.ronghuanet.car.domain.CarResources;
import cn.ronghuanet.car.query.CarResourcesQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carResources")
public class CarResourcesController {
    @Autowired
    public ICarResourcesService carResourcesService;


    /**
     * 保存和修改公用的
     * @param carResources  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody CarResources carResources){
        if( carResources.getId()!=null)
            carResourcesService.update(carResources);
        else
            carResourcesService.insert(carResources);
        return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteOne(@PathVariable("id") Long id){
        carResourcesService.delete(id);
        return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id)
    {
        CarResources carResources = carResourcesService.selectById(id);
        return AjaxResult.success(carResources);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        List< CarResources> list = carResourcesService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public AjaxResult queryPage(@RequestBody CarResourcesQuery query)
    {
        PageInfo<CarResources> pageList = carResourcesService.pageList(query);
        return AjaxResult.success(pageList);

    }

    /**
   * 批量删除
   * @return
   */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        carResourcesService.batchDelete(ids);
        return AjaxResult.success();
    }

    @PutMapping("/batch")
    public AjaxResult batchSave(@RequestBody CarResourcesDTO dto){
        carResourcesService.batchSave(dto);
        return AjaxResult.success();
    }

    @GetMapping("/carid/{carId}")
    public AjaxResult getCarResourceByCarId(@PathVariable("carId")Long carId){
        List<CarResources> list = carResourcesService.getCarResourceByCarId(carId);
        return AjaxResult.success(list);
    }
}
